package cn.youframe.frame.tag;

import cn.youframe.frame.beans.CommTypeValVO;
import cn.youframe.frame.cache.CacheMgr;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;

/**
 * User: Administrator
 * DateTime: 12-4-25: 下午3:28
 */
public class SelectTag extends TagSupport {
    private CacheMgr manager= CacheMgr.getInstance();
    private String tagId;//下拉框的ID
    private String tagName;//下拉框的Name
    private String selectFlag;//是否显示请选择，默认没有
    private String onclick;//点击事件
    private String onchange;//改变下拉框值的事件
    private String onfocus;//获得焦点事件
    private String onblur;//失去焦点事件
    private String disabled;//是否可用 true不可用
    private String title;//提示的Title
    private String typeId;//类型ID
    private String startWith;//过滤条件，显示开头的val值
    private String selected;//需要选中的值
    private String style;//下拉框的样式
    public int doEndTag() throws JspException {
        try {
            StringBuffer buf = new StringBuffer();
            buf.append("<select ");
            if(tagName!=null && !(tagName.trim()).equals("")){
                buf.append("name='").append(tagName).append("' ");
            }
            if(tagId!=null && !(tagId.trim()).equals("")){
                buf.append("id='").append(tagId).append("' ");
            }
            if(onclick!=null && !(onclick.trim()).equals("")){
                buf.append("onclick='").append(onclick).append("' ");
            }
            if(onchange!=null && !(onchange.trim()).equals("")){
                buf.append("onchange='").append(onchange).append("' ");
            }
            if(title!=null && !(title.trim()).equals("")){
                buf.append("title='").append(title).append("' ");
            }
            if(onfocus!=null && !(onfocus.trim()).equals("")){
                buf.append("onfocus='").append(onfocus).append("' ");
            }
            if(onblur!=null && !(onblur.trim()).equals("")){
                buf.append("onblur='").append(onblur).append("' ");
            }
            if(disabled!=null && !(disabled.trim()).equals("")){
                buf.append("disabled='disabled' ");
            }
            if(style!=null && !(style.trim()).equals("")){
                buf.append("class='").append(style).append("' ");
            }
            buf.append("> ");
            if(selectFlag!=null && (selectFlag.trim()).equals("true")){
                buf.append("<option value=''>-请选择-</option>");
            }
            SortedMap<String,CommTypeValVO> valMap=(SortedMap<String,CommTypeValVO>)manager.get(typeId);
            if(valMap!=null){
                Iterator<Map.Entry<String,CommTypeValVO>> valIt=valMap.entrySet().iterator();
                CommTypeValVO valVo=null;
                if(startWith!=null && !(startWith.trim()).equals("")){
                    while (valIt.hasNext()) {
                        valVo = valIt.next().getValue();
                        if (valVo.getValId().startsWith(startWith)) {
                            buf.append("<option value='").append(valVo.getValId()).append("'");
                            if(selected!=null && (selected.trim()).equals(valVo.getValId())){
                                buf.append(" selected='selected' ");
                            }
                            buf.append(">").append(valVo.getValName()).append("</option>");
                        } else {
                            continue;
                        }
                    }
                }else{
                    while (valIt.hasNext()) {
                        valVo = valIt.next().getValue();
                        buf.append("<option value='").append(valVo.getValId()).append("'");
                        if(selected!=null && (selected.trim()).equals(valVo.getValId())){
                            buf.append(" selected='selected' ");
                        }
                        buf.append(">").append(valVo.getValName()).append("</option>");
                    }
                }
            }
            buf.append("</select> ");
            pageContext.getOut().write(buf.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.doEndTag();
    }

    public void setTagName(String tagName) {
        this.tagName = tagName;
    }

    public void setTagId(String tagId) {
        this.tagId = tagId;
    }

    public void setSelectFlag(String selectFlag) {
        this.selectFlag = selectFlag;
    }

    public void setOnclick(String onclick) {
        this.onclick = onclick;
    }

    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setStartWith(String startWith) {
        this.startWith = startWith;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    public void setOnfocus(String onfocus) {
        this.onfocus = onfocus;
    }

    public void setOnblur(String onblur) {
        this.onblur = onblur;
    }

    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getTagName() {
        return tagName;
    }

    public String getTagId() {
        return tagId;
    }

    public String getSelectFlag() {
        return selectFlag;
    }

    public String getOnclick() {
        return onclick;
    }

    public String getOnchange() {
        return onchange;
    }

    public String getTitle() {
        return title;
    }

    public String getStartWith() {
        return startWith;
    }

    public String getTypeId() {
        return typeId;
    }

    public String getSelected() {
        return selected;
    }

    public String getOnfocus() {
        return onfocus;
    }

    public String getOnblur() {
        return onblur;
    }

    public String getDisabled() {
        return disabled;
    }

    public String getStyle() {
        return style;
    }

}
